package com.qfedu.web;

import com.qfedu.pojo.Account;
import com.qfedu.pojo.CreditCard;
import com.qfedu.pojo.Infor;
import com.qfedu.pojo.User;
import com.qfedu.service.AccountService;
import com.qfedu.service.CreditCardService;
import com.qfedu.service.InforService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.CreditCardServiceImpl;
import com.qfedu.service.impl.InforServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Random;

@WebServlet("/creditCard")
public class CreditCardServlet extends BaseServlet {
    CreditCardService creditCardService = new CreditCardServiceImpl();
    AccountService accountService = new AccountServiceImpl();
    InforService inforService = new InforServiceImpl();

    //点击信用卡，根据手机号判断是否有信用卡
    protected void credit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据手机号查
        User user = (User) req.getSession().getAttribute("user");
        if (user != null) {
            String phone = user.getPhone();
            CreditCard cardPhone = creditCardService.findCreditCard(phone);
            if (cardPhone != null) {
                resp.sendRedirect("/page/credit_success.jsp");
            } else {
                resp.sendRedirect("/page/credit_card.jsp");
            }
        } else {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }
    }

    //申请信用卡
    protected void credit2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String idcode = req.getParameter("idcode");
        String phone = req.getParameter("phone");
        Infor infor = new Infor(phone, username, idcode);
        if (inforService.existsInfor(infor)) {
            CreditCard creditCard = new CreditCard();
            creditCard.setName(username);
            creditCard.setIdcode(idcode);
            creditCard.setPhone(phone);
            creditCard.setMaxOverdraft(5000);

            //随机生成卡号
            String s = "";
            for (int i = 0; i < 6; i++) {
                Random random = new Random();
                int num = random.nextInt(9);
                s += num;
            }
            creditCard.setCardnumber(s);
            HttpSession session = req.getSession();
            session.setAttribute("cardnumber", s);
            //将信息添加到数据库
            int row = creditCardService.addCreditCard(creditCard);
            if (row > 0) {
                resp.sendRedirect("/page/shenqing_success.jsp");
            } else {
                resp.getWriter().write("申请失败");
            }
        } else {
            req.getSession().setAttribute("name", "未实名认证");
            resp.sendRedirect("/page/credit_card.jsp");
        }

    }

    //还款
    protected void credit3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据电话号码查
        User user = (User) req.getSession().getAttribute("user");
        String phone = user.getPhone();
        Account source = accountService.findAccountPhone(phone);
        //判断密码是否正确
        String paypassword = source.getPaypassword();
        String password = req.getParameter("password");
        if (paypassword.equals(password)) {

            //根据卡号查
            String cardnumber = req.getParameter("cardnumber");
            CreditCard target = creditCardService.findCardsnumber(cardnumber);
            //判断卡号是否为空
            if (target == null) {
                req.getSession().setAttribute("ka", "卡号不存在");
                req.getRequestDispatcher("/page/repayment.jsp").forward(req, resp);
            }
            String money = req.getParameter("money");
            if (Integer.parseInt(money) <= 0) {
                req.getSession().setAttribute("money", "请重新输入金额");
                req.getRequestDispatcher("/page/repayment.jsp").forward(req, resp);

            }

            //null
            if (password == "" || cardnumber == "" || money == "") {
                //加
                req.getSession().setAttribute("huankuan", "请完善信息");
                req.getRequestDispatcher("/page/repayment.jsp").forward(req, resp);
            }
            //判断账户余额
            BigDecimal m = source.getMoney();
            int mm = m.intValue();
            if (Integer.parseInt(money) >= mm) {
//                resp.getWriter().write("您的余额不足，无法还款");

                req.getSession().setAttribute("ff", "您的余额不足，还款失败");
                req.getRequestDispatcher("/page/repayment.jsp").forward(req, resp);
            } else {
                creditCardService.transfer(source, target, Integer.parseInt(money));
                BigDecimal moneys = source.getMoney();
                req.getSession().setAttribute("cc", moneys.intValue() - Integer.parseInt(money));
                req.getSession().setAttribute("c", "还款成功");
                resp.sendRedirect("/page/repayment_success.jsp");
                // resp.getWriter().write("还款成功");
            }
        } else {
            //加
            req.getSession().setAttribute("huankuan", "密码错误");
            req.getRequestDispatcher("/page/repayment.jsp").forward(req, resp);
        }
    }

    //取现
    protected void credit4(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cardnumber = req.getParameter("cardnumber");
        String money = req.getParameter("money");
        String password = req.getParameter("password");
        //把要透支的钱存到域中
        if (cardnumber != "" && money != "" && password != "") {
            //查询密码是否正确
            Account account = accountService.findPassword();
            if (account != null && password.equals(account.getPaypassword())) {
                //密码正确,提现成功
                //根据卡号查
                CreditCard source = creditCardService.findCardsnumber(cardnumber);
                BigDecimal balance = source.getBalance();
                int balances = balance.intValue();
                //把查到的信用卡中的钱存到域中
                req.getSession().setAttribute("balance", balances);

                //根据手机号查账户
                User user = (User) req.getSession().getAttribute("user");
                String phone = user.getPhone();
                Account target = accountService.findAccountPhone(phone);
                accountService.transefer2(source, target, Integer.parseInt(money));
                resp.sendRedirect("/page/quxian_success.jsp");
            } else {
                //错误
                //resp.getWriter().write("密码错误");
                req.getSession().setAttribute("qx", "密码错误");
                resp.sendRedirect("/page/quxian.jsp");
            }
        } else {
            req.getSession().setAttribute("qx", "请完善信息");
            resp.sendRedirect("/page/quxian.jsp");
        }

    }

    //查询额度
    protected void edu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据手机号查询钱,判断额度
        User user = (User) req.getSession().getAttribute("user");
        String phone = user.getPhone();
        Account account = accountService.findAccountPhone(phone);
        BigDecimal money = account.getMoney();
        int m = money.intValue();
        if (m < 5000) {
            req.getSession().setAttribute("money", 5000);
            resp.sendRedirect("/page/limit.jsp");
        } else if (m >= 5000 && m < 10000) {
            req.getSession().setAttribute("money", 10000);
            resp.sendRedirect("/page/limit.jsp");
        } else if (m >= 10000 && m < 50000) {
            req.getSession().setAttribute("money", 50000);
            resp.sendRedirect("/page/limit.jsp");
        } else {
            req.getSession().setAttribute("money", m * 1.5);
            resp.sendRedirect("/page/limit.jsp");
        }
    }

    //透支额度
    protected void touzhi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int money2 = 0;
//        int balance = 0;
        //根据卡号查
        User user = (User) req.getSession().getAttribute("user");
        String phone = user.getPhone();
        CreditCard creditCard = creditCardService.findCreditCard(phone);
        BigDecimal balance1 = creditCard.getBalance();
//        CreditCard cardsnumber = creditCardService.findCardsnumber("466260");
//        BigDecimal balance1 = cardsnumber.getBalance();
//        money2 = (int) req.getSession().getAttribute("money2");
//        balance = (int) req.getSession().getAttribute("balance");
//        if (money2 != 0 || balance != 0) {
//            int a = -(balance - money2);
//            req.getSession().setAttribute("m3", a);
//            resp.sendRedirect("/page/touzhi.jsp");
//        } else {
//            req.getSession().setAttribute("m3", "请稍后查询");
//            resp.sendRedirect("/page/touzhi.jsp");
//        }
        req.getSession().setAttribute("m3", balance1);
        resp.sendRedirect("/page/touzhi.jsp");
        //根据身份证查信用卡
//        CreditCard idcodeCredit = creditCardService.findIdcodeCredit("152104199909011234");
    }

}
